package rdp.cmd.base;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rdp.RDPUtil;

public class CMDBase {
    public boolean execute(CommandSender sender, String[] args) {
        boolean isAdmin = sender.hasPermission("minecraft");
        boolean isPlayer = sender instanceof Player;
        Player player = isPlayer ? (Player) sender : null;
        Player target = null;

        if (args.length == 1) {
            args = new String[]{"base", "menu"};
        }
        if (getCmd(args[1]) == "menu") {
            if (isPlayer) {
                player.openInventory(getMenu());
                return true;
            } else
                args = new String[]{"base", "help"};
        }
        if (getCmd(args[1]) == "help") {
            RDPUtil.SEND(sender, getHelp());
            return true;
        }
        switch (getCmd(args[1])) {
            case "time":
                if (args.length > 2 && isAdmin)
                    player.chat("/time set " + setTime(Integer.parseInt(args[2].replace(":", ""))));
                int h = getTime() / 100;
                int m = getTime() % 100;
                RDPUtil.SEND(sender, "§e时间: §f" + (h < 10 ? "0" + h : "" + h) + ":" + (m < 10 ? "0" + m : "" + m));
                break;
            case "position":
                target = player;
                if (args.length > 2 && isAdmin)
                    target = Bukkit.getPlayer(args[2]);
                if (target == null) {
                    RDPUtil.SEND(sender, "§4目标不存在。");
                    break;
                }
                Location location = target.getLocation();
                RDPUtil.SEND(sender, new String[]{"§e玩家: §f" + target.getName() + "\n" + "§e世界: §f" + location.getWorld().getName() + "\n" + "§e坐标: §f" + (int) location.getX() + " , " + (int) location.getY() + " , " + (int) location.getZ()});
                break;
            case "enderchest":
                if (isPlayer) {
                    target = player;
                    if (args.length > 2 && isAdmin)
                        target = Bukkit.getPlayer(args[2]);
                    if (target == null) {
                        RDPUtil.SEND(sender, "§4目标不存在。");
                        break;
                    }
                    player.openInventory(target.getEnderChest());
                    break;
                }
                RDPUtil.SEND(sender, "§4您无法使用该指令。");
                break;
            case "inventory":
                if (isPlayer && isAdmin) {
                    if (args.length > 2 && isAdmin)
                        target = Bukkit.getPlayer(args[2]);
                    if (target == null) {
                        RDPUtil.SEND(sender, "§4目标不存在。");
                        break;
                    }
                    player.openInventory(target.getInventory());
                    break;
                }
                RDPUtil.SEND(sender, "§4您无法使用该指令。");
                break;
            default:
                RDPUtil.SEND(sender, "§4指令不存在。");
        }
        return true;
    }

    private String getCmd(String str) {
        List<String[]> list = new ArrayList(){{
            add(new String[]{"?", "help"});
            add(new String[]{"!", "menu"});
            add(new String[]{"clock", "time"});
            add(new String[]{"pos", "position"});
            add(new String[]{"ec", "enderchest"});
            add(new String[]{"inv", "inventory"});
        }};

        for (String[] strs : list) {
            if (RDPUtil.CHECK(str, strs))
                return strs[strs.length - 1];
        }
        return null;
    }

    private String[] getHelp() {
        return new String[]{
                "§b/rdp base help§f - 获取基础指令组帮助。",
                "§b/rdp base menu§f - 打开基础菜单。",
                "§b/rdp base time§f - 获取主世界时间。",
                "§b/rdp base position §f - 获取玩家坐标。",
                "§b/rdp base enderchest §f - 打开玩家末影箱。",
                "§b/rdp base inventory §f - 打开玩家背包。"
        };
    }

    private Inventory getMenu() {
        Inventory inv = Bukkit.createInventory(null, 54, "基础菜单");

        ItemStack blank = new ItemStack(Material.GLASS_PANE);
        ItemMeta blankIM = blank.getItemMeta();
        blankIM.setDisplayName(" ");
        blank.setItemMeta(blankIM);

        for (int i = 0; i < inv.getSize(); i++)
            inv.setItem(i, blank);

        Map exit = new HashMap() {{
            put("MATERIAL", Material.BARRIER);
            put("NAME", "§c返回 §f| §c帮助");
            put("LORE", new ArrayList<String>() {{
                add("§b左击§a返回主菜单");
                add("§b右击§a查看帮助");
            }});
            put("LEFT", "/rdp menu");
            put("RIGHT", "/rdp base help");
        }};

        Map time = new HashMap() {{
            put("MATERIAL", Material.CLOCK);
            put("NAME", "§c时间");
            put("LORE", new ArrayList<String>() {{
                add("§b点击§a查看主世界时间");
            }});
            put("LEFT", "/rdp base time");
            put("RIGHT", "/rdp base time");
        }};

        Map pos = new HashMap() {{
            put("MATERIAL", Material.COMPASS);
            put("NAME", "§c坐标");
            put("LORE", new ArrayList<String>() {{
                add("§b点击§a查看当前坐标");
            }});
            put("LEFT", "/rdp base position");
            put("RIGHT", "/rdp base position");
        }};


        Map ec = new HashMap() {{
            put("MATERIAL", Material.ENDER_CHEST);
            put("NAME", "§c坐标");
            put("LORE", new ArrayList<String>() {{
                add("§b点击§a打开末影箱");
            }});
            put("LEFT", "/rdp base enderchest");
            put("RIGHT", "/rdp base enderchest");
        }};

        inv.setItem(12, RDPUtil.ITEM(time));
        inv.setItem(13, RDPUtil.ITEM(pos));
        inv.setItem(14, RDPUtil.ITEM(ec));
        inv.setItem(inv.getSize()-1, RDPUtil.ITEM(exit));
        return inv;
    }

    public Integer setTime(int time) {
        int h = time / 100;
        int m = time % 100;
        return (int) (h - 6 > 0 ? (h - 6) * 1000 : (h + 18) * 1000 + m / 0.06);
    }

    public Integer getTime() {
        int time = Integer.parseInt(String.valueOf(Bukkit.getWorld("world").getTime()));
        int h = time / 1000 + 6 > 23 ? time / 1000 - 18 : time / 1000 + 6;
        Double m = time % 1000 * 0.06;
        return (int) (h * 100 + m);
    }

}
